<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
      class="form-search"
    >
      <el-form-item label="设备类型名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入设备类型名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['base:prodEquipmentType:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:prodEquipmentType:edit']"
          >编辑</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:prodEquipmentType:delete']"
          >删除</el-button
        >
      </el-col> -->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="tableData"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备类型编号" align="center" prop="code">
        <template #default="scope">
          <el-link type="primary" @click="handleDetail(scope.row)">{{
            scope.row.code
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column
        label="设备类型名称"
        align="center"
        prop="name"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="批记录模板名称"
        align="center"
        prop="sopTemplateName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="创建人"
        align="center"
        prop="createBy"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="修改人"
        align="center"
        prop="updateBy"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="修改时间"
        align="center"
        prop="updateTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            v-hasPermi="['base:prodEquipmentType:edit']"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            v-hasPermi="['base:prodEquipmentType:delete']"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      width="700px"
      append-to-body
    >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="gutter">
          <el-col :span="span">
            <el-form-item label="设备类型编号" prop="code">
              <code-input
                v-if="!disabledForm"
                v-model="form.code"
                code="EQUIPMENT_TYPE_CODE"
                :disabled="!!form.id"
                placeholder="请输入设备类型编号"
              />
              <div v-else>{{ form.code }}</div>
            </el-form-item>
          </el-col>
          <el-col :span="span">
            <el-form-item label="设备类型名称" prop="name">
              <el-input
                v-if="!disabledForm"
                v-model="form.name"
                placeholder="请输入设备类型名称"
              />
              <div v-else>{{ form.name }}</div>
            </el-form-item>
          </el-col>
          <el-col :span="span">
            <el-form-item label="批记录模板" prop="sopTemplateCode">
              <el-input
                v-if="!disabledForm"
                v-model="form.sopTemplateCode"
                placeholder="请选择批记录模板"
              />
              <div v-else>{{ form.sopTemplateCode }}</div>
            </el-form-item>
          </el-col>
          <el-col :span="span">
            <el-form-item label="清洁有效期" prop="cleanExpirePeriod">
              <el-input-number
                v-if="!disabledForm"
                v-model="form.cleanExpirePeriod"
                class="mx-4"
                :min="0"
                :max="999"
                controls-position="right"
              />
              <div v-else>{{ form.cleanExpirePeriod }}</div>
            </el-form-item>
          </el-col>
          <el-col :span="span">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-if="!disabledForm"
                v-model="form.remark"
                :maxlength="200"
                show-word-limit
                type="textarea"
                placeholder="请输入内容"
              ></el-input>
              <div v-else>{{ form.remark }}</div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button v-if="!disabledForm" type="primary" @click="submitForm"
            >确 定</el-button
          >
          <el-button v-if="!disabledForm" @click="cancel">取 消</el-button>
          <el-button v-if="disabledForm" @click="cancel">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Dict">
import {
  listType,
  getType,
  delType,
  addType,
  updateType,
} from "@/api/base/equipment/equipmentTypeApi";

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

const tableData = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const disabledForm = ref(false);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
  },
  rules: {
    name: [
      { required: true, message: "设备类型名称不能为空", trigger: "blur" },
    ],
    code: [
      { required: true, message: "设备类型编号不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询设备类型列表 */
function getList() {
  loading.value = true;
  listType(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (response) => {
      tableData.value = response.rows;
      total.value = response.total;
      loading.value = false;
    }
  );
}

const gutter = computed(() => {
  return disabledForm.value ? 20 : 0;
});
const span = computed(() => {
  return disabledForm.value ? 12 : 24;
});
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    name: undefined,
    remark: undefined,
    sopTemplateCode: undefined,
    sopTemplateName: undefined,
    cleanExpirePeriod: 0,
  };
  proxy.resetForm("formRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  disabledForm.value = false;
  reset();
  open.value = true;
  title.value = "添加设备类型";
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

function handleDetail(row) {
  disabledForm.value = true;
  reset();
  const id = row.id;
  getType(id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "查看设备类型";
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  disabledForm.value = false;
  const typeId = row.id || ids.value;
  getType(typeId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "编辑设备类型";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        const params = {
          id: form.value.id,
          code: form.value.code,
          name: form.value.name,
          remark: form.value.remark,
          sopTemplateCode: form.value.sopTemplateCode,
          sopTemplateName: form.value.sopTemplateName,
          cleanExpirePeriod: form.value.cleanExpirePeriod,
        };
        updateType(params).then((response) => {
          proxy.$modal.msgSuccess("编辑成功");
          open.value = false;
          getList();
        });
      } else {
        addType(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const idList = row.id || ids.value.join(",");
  const codes =
    row.code ||
    ids.value
      .map((id) => tableData.value.find((item) => item.id === id)?.code)
      .join(", ");
  proxy.$modal
    .confirm('是否确认删除设备类型编号为"' + codes + '"的数据项？')
    .then(function () {
      return delType({ ids: idList });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>
